# -*- coding: utf-8 -*-
import os
import sys

# Setup Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TradingPlatform_Server.settings')

import django
django.setup()

from Server.models import User
from django.contrib.auth.hashers import make_password, check_password

print("=" * 60)
print("Fixing Admin Password")
print("=" * 60)

try:
    # Get admin user
    admin = User.objects.get(phone='13800138000')
    print("\nFound admin user:")
    print("  ID:", admin.id)
    print("  Username:", admin.username)
    print("  Phone:", admin.phone)
    print("  Is Admin:", admin.is_admin)
    
    # Reset password
    new_password = 'admin123'
    admin.password = make_password(new_password)
    admin.is_admin = True
    admin.save()
    
    print("\nPassword reset!")
    
    # Verify
    admin.refresh_from_db()
    if check_password(new_password, admin.password):
        print("Verification: SUCCESS")
    else:
        print("Verification: FAILED")
    
    print("\n" + "=" * 60)
    print("Login Credentials:")
    print("  Phone: 13800138000")
    print("  Password: admin123")
    print("=" * 60)
    
except User.DoesNotExist:
    print("\nERROR: Admin user not found!")
except Exception as e:
    print("\nERROR:", str(e))
    import traceback
    traceback.print_exc()

